package com.bw.utils;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * 字符串工具类
 *      以后只要是字符串相关的处理操作都来找它
 */
public class StringUtil {


    /**
     * *********必用*********
     * 判断源字符串是否有值，空引号和空格也算没值
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        return str ==null || str.trim().length()<=0;
    }

    /**
     * 判断是否为手机号码
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone){
        String reg = "1[3|5|7|8|9]\\d{9}";
        return phone.matches(reg);
    }

    /**
     * 判断是否为电子邮箱
     * @param string
     * @return
     */
    public static boolean isEmail(String string){
        String reg = "\\w+@\\w+(.com|.cn|.com.cn)";
        return string.matches(reg);
    }

    /**
     * 判断是否全部为字母
     * @return
     */
    public static boolean isLetter(String str){
        String reg = "[a-zA-Z]+";
        return str.matches(reg);
    }

    /**
     * 判断是否是小数
     * @return
     */
    public static boolean isFloat(String str){
        //正则验证是否是一个数值(整数、小数)
        //String reg = "(\\d+\\.\\d+|\\d+)";
        String reg = "\\d+\\.\\d+";
        return str.matches(reg);
    }

    /**
     * 判断是否是数字
     * @return
     */
    public static boolean isNumber(String str){
        //正则验证是否是一个数值(整数、小数)
        String reg = "(\\d+\\.\\d+|\\d+)";
        return str.matches(reg);
    }


    /**
     * 将\n\r替换成一个\n
     * 将\n结尾的这行文本用<p></p>标签包起来
     * 将单个\r字符使用<br/>标签替换
     * @param text
     * @return
     */
    public static String toHtml(String text){

        String[] split = text.split("(\\n\\r|\\n)");

        String str1 = "";
        for (String s : split) {
            str1+="<p>"+s+"</p>";
        }
        String s = str1.replaceAll("\\r", "<br/>");

        return s;
    }


    /*获取n位随机英文字符串
            获取n位随机英文和数字字符串
    获取n个随机中文字符串*/

    public static String getRandomString(int length){
//        获取n位随机英文字符串
        //A-Z 65 90
        //a-z 97 122
        StringBuffer stringBuffer = new StringBuffer();

        for (int i = 0; i < length; i++) {
            int num = (int) (Math.random()*123);
            if ((num>=65 && num <= 90) || (num>=97 && num <= 122)){
                char c = (char) num;
                stringBuffer.append(c);
            }else{
                i--;
            }
        }
        return stringBuffer.toString();

    }


    /**
     * 获取n个随机中文字符串
     * @return
     */
    public static String getAChinese(int length) {
        StringBuilder str = new StringBuilder();
        int hs = 0 ;
        int ls = 0;
        Random random = new Random();
        for(int i = 0 ; i < length ; i++){
            hs = (176 + Math.abs(random.nextInt(39)));
            ls = (161 + Math.abs(random.nextInt(93)));
            byte[] b = new byte[2];
            b[0] = (new Integer(hs).byteValue());
            b[1] = (new Integer(ls).byteValue());
            try {
                str.append(new String(b, "GBk"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return str.toString();
    }


    public static void main(String[] args) {
        System.out.println(getAChinese(10));
    }

}
